﻿function AirTypeAnalyseLine(container, config) {
    this.TimePoint = [];
    this.ForTime = [];
    this.ForecastScheme = [];
    this.Monitor = [];
    this.ForecastInForecastModel0 = [];
    this.ForecastInForecastModel1 = [];
    this.ForecastInForecastModel2 = [];
    this.ForecastInForecastModel3 = [];
    this.ForecastInForecastModel4 = [];
    this.ForecastInForecastModel5 = [];
    this.FloatingInForecastModel0 = [];
    this.FloatingInForecastModel1 = [];
    this.FloatingInForecastModel2 = [];
    this.FloatingInForecastModel3 = [];
    this.FloatingInForecastModel4 = [];
    this.FloatingInForecastModel5 = [];
    this.rangeseries = [];
    this.series = [];
    this.seriesParam = config["seriesParam"] || [];
    this.height = config["height"] || null;
    this.width = config["width"] || null;
    this.xwidth = config["xwidth"] || 60;
    this.stationName = config["stationName"] || "";
    this.title = config["title"] || "";
    this.useDefaultTip = config["useDefaultTip"] || false;
    this.doubleAxis = config["doubleAxis"] || false;
    this.coAxis = config["coAxis"] || false;
    this.yAxis = [];


    this.container = container;
}

AirTypeAnalyseLine.prototype.tooltipFormatter = function (tooltip) {
    var ret = '<middle>' + tooltip.x + '</middle><br>';
    ret += '<table>';

    Highcharts.each(tooltip.points, function (point) {
        var series = point.series;
        ret += '<tr><td><span style="color:' + series.color + '">\u25CF</span> ' + series.name + '</td><td>' + Highcharts.pick(point.point.value, point.y) + '</td></tr>';
    });

    ret += '</table>';

    return ret;

};

AirTypeAnalyseLine.prototype.getStep = function () {
    var _ = this,
        width = $("#" + _.container).width(),
        count = _.TimePoint.length,
        num = 1,
        xWidth = _.xwidth;
    while (xWidth * count > width) {
        num = num * 2;
        count = count / 2;
    }
    return num;
}

AirTypeAnalyseLine.prototype.getChartOptions = function () {
    var _ = this;
    return {
        chart: {
            renderTo: _.container,
            height: _.height,
            width: _.width
        },
        lang: {
            contextButtonTitle: '导出图片',
            loading: '加载中',
            noData: '没有数据'
        },
        title: {
            text: '空气质量等级对比分析图'
        },
        credits: {
            enabled: false
        },
        xAxis: [{
            categories: _.TimePoint, //_.ForTime,
            tickInterval: _.getStep()
        }],
        yAxis: {
            title: {
                text: '偏差'
            },
            labels: {
                style: {
                    fontSize: '10px'
                }
            },
            plotLines: [{
                value: 0,
                width: 1,
                color: '#808080'
            }
            ],
            min: -5,
            max: 5,
            tickInterval: 1
        },
        tooltip: {
            shared: true,
            //crosshairs: true,
            crosshairs: [
             {
               width: 1,
             }
            ],
            useHTML: true,
            formatter: function () {
                return _.tooltipFormatter(this);
            }
        },
        series: _.series
    }
};

AirTypeAnalyseLine.prototype.onChartLoad = function () {
};

AirTypeAnalyseLine.prototype.createChart = function () {
    var _ = this;
    this.chart = new Highcharts.Chart(_.getChartOptions(), function (chart) {
        _.onChartLoad();
    });
};

AirTypeAnalyseLine.prototype.initData = function (jsonData) {
    var _ = this;
    if (jsonData.length > 0) {
        for (var i in jsonData) {
            var item = jsonData[i];
            if (_.TimePoint.indexOf(item.ForTime) == -1) {
                _.TimePoint.push(item.ForTime);
            }
            if (_.rangeseries[item.ForecastModelId] == undefined) {
                _.rangeseries[item.ForecastModelId] = {
                    name: item.ForecastModelId,
                    data: []
                };
            }
            _.rangeseries[item.ForecastModelId].data.push(item["AQILevel_Error"]);
        }
        _.series = _.getSeries(_.series);
        _.createChart();
    }
    else {
        return "no data";
    }
    //var _ = this;
    //if (jsonData.length > 0) {
    //    var d = jsonData;
    //    for (var i = 0; i < jsonData.length; i++) {

    //        _.TimePoint.push(GetTimeFromJsonString(d[i].TimePoint).format("MM月dd日"));
    //        _.ForTime.push(GetTimeFromJsonString(d[i].ForTime).format("MM月dd日"));
    //        _.ForecastScheme.push(d[i].ForecastScheme);
    //        _.Monitor.push(d[i].Monitor);
    //        _.ForecastInForecastModel0.push(d[i].ForecastInForecastModel0);
    //        _.ForecastInForecastModel1.push(d[i].ForecastInForecastModel1);
    //        _.ForecastInForecastModel2.push(d[i].ForecastInForecastModel2);
    //        _.ForecastInForecastModel3.push(d[i].ForecastInForecastModel3);
    //        _.ForecastInForecastModel5.push(d[i].ForecastInForecastModel5);
    //        _.FloatingInForecastModel0.push(d[i].FloatingInForecastModel0);
    //        _.FloatingInForecastModel1.push(d[i].FloatingInForecastModel1);
    //        _.FloatingInForecastModel2.push(d[i].FloatingInForecastModel2);
    //        _.FloatingInForecastModel3.push(d[i].FloatingInForecastModel3);
    //        _.FloatingInForecastModel5.push(d[i].FloatingInForecastModel5);

    //    }
    //    _.series = _.getSeries(_.series);
    //    _.createChart();
    //}
    //else {
    //    return "no data";
    //}
};

AirTypeAnalyseLine.prototype.getSeries = function (series, people) {
    var _ = this;
    series = [];
    if (_.rangeseries["0"] != undefined) {
        series.push({
            id: 'model0Line',
            name: "多模式择优",
            color: '#9900CC',
            type: 'spline',
            data: _.rangeseries["0"].data //_.FloatingInForecastModel0
        });
    }
    if (_.rangeseries["1"] != undefined) {
        series.push({
            id: 'model1Line',
            name: "多元回归模型",
            color: '#7CB5EC',
            type: 'spline',
            data: _.rangeseries["1"].data // _.FloatingInForecastModel1
        });
    }
    if (_.rangeseries["2"] != undefined) {
        series.push({
            id: 'model2Line',
            name: "同期回归模型",
            color: '#90ED7D',
            type: 'spline',
            data: _.rangeseries["2"].data //_.FloatingInForecastModel2
        });
    }
    if (_.rangeseries["3"] != undefined) {
        series.push({
            id: 'model3Line',
            name: "BP神经网络",
            color: '#F45B5B',
            type: 'spline',
            data: _.rangeseries["3"].data //_.FloatingInForecastModel3
        });
    }
    if (_.rangeseries["5"] != undefined) {
        series.push({
            id: 'model5Line',
            name: "聚类回归模型",
            color: '#D3BA77',
            type: 'spline',
            data: _.rangeseries["5"].data //_.FloatingInForecastModel5
        });
    }
    return series;
}

